/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utilidades;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
/**
 *
 * @author humberto
 */
public class utils {
    
    public static void autoDimensionarTabla(JTable tabla) {
		tabla.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        int margin = 5;

        for (int i = 0; i < tabla.getColumnCount(); i++) {

            int vColIndex = i;

            if (vColIndex != 2)
            {
                DefaultTableColumnModel colModel = (DefaultTableColumnModel) tabla.getColumnModel();
                TableColumn col = colModel.getColumn(vColIndex);
                int width = 0;

                TableCellRenderer renderer = col.getHeaderRenderer();

                if (renderer == null) {
                    renderer = tabla.getTableHeader().getDefaultRenderer();
                }

                Component comp = renderer.getTableCellRendererComponent(tabla, col.getHeaderValue(), false, false, 0, 0);

                width = comp.getPreferredSize().width;

                for (int r = 0; r < tabla.getRowCount(); r++) {
                    renderer = tabla.getCellRenderer(r, vColIndex);
                    comp     = renderer.getTableCellRendererComponent(tabla, tabla.getValueAt(r, vColIndex), false, false,
                            r, vColIndex);
                    width = Math.max(width, comp.getPreferredSize().width);
                }

                width += 2 * margin;

                col.setPreferredWidth(width);
            }
        }

        ((DefaultTableCellRenderer) tabla.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);

        tabla.getTableHeader().setReorderingAllowed(false);

    }

    public static boolean verificarString(String entrada)
    {
        String patron = "[A-Za-z\\s]+";
        return Pattern.matches(patron, entrada);
    }

    public static boolean verificarInteger(String entrada)
    {
        String patron = "[0-9]+";
        return Pattern.matches(patron, entrada);
    }
    
     public static boolean verificarEspacioVacio(String entrada)
    {
        return (entrada.isEmpty());
    }

    public static String[] aVectorString(ArrayList<String[]> array,int indice)
    {
        String vector[] = new String[array.size()];
        for (int i = 0; i < array.size(); i++ )
            vector[i] = array.get(i)[indice];
        return vector;
    }

    public static Point  getPosition(JFrame objeto)
    {

        Dimension tamanhoObjeto = objeto.getSize();
        Dimension tamanhoPantalla = Toolkit.getDefaultToolkit().getScreenSize();

        int x = (tamanhoPantalla.width - tamanhoObjeto.width)/2;
        int y = (tamanhoPantalla.height - tamanhoObjeto.height)/2;
        
        return new Point(x, y);
    }

    public static String getPDFReader() throws FileNotFoundException, IOException
    {
        String reader = "";
        File f = new File( "setting.conf" );
        BufferedReader entrada = new BufferedReader( new FileReader( f ) );
        if ( f.exists() ) 
            reader = entrada.readLine();
        return reader;
    }

    public static String getLastNumberDoc() throws FileNotFoundException, IOException
    {
        String number = "1";
        File f = new File( "values.conf" );
        BufferedReader entrada = new BufferedReader( new FileReader( f ) );
        if ( f.exists() )
        {
            number = entrada.readLine();
            Integer nextNumber = Integer.valueOf(number) + 1;
            number =  nextNumber.toString();
        }
        return number;
    }

    public static void setLastNumberDoc(String numDoc) throws FileNotFoundException, IOException
    {
        File f = new File( "values.conf" );
        BufferedWriter salida = new BufferedWriter( new FileWriter( f ) );
        salida.write(numDoc);
        salida.close();
    }
    
    public static String formatID(int id, int size){
        String sid = String.valueOf(id);
        String zeros = "";
        for(int i = 0; i < size - sid.length(); i++) zeros += "0";
        return  zeros + sid;
    } 
    
    public static String formatID(String id, int size){
        String zeros = "";
        for(int i = 0; i < size - id.length(); i++) zeros += "0";
        return  zeros + id;
    }
    
    public static void mostrarVentana(JFrame ventana){
    ventana.setVisible(true);
    }
    
    public static void ocultarVentana(JFrame ventana){
    ventana.setVisible(false);
    }

}
